package com.gitease.quickhub.service.impl;

import com.gitease.quickhub.model.Commit;
import com.gitease.quickhub.model.Repository;
import com.gitease.quickhub.model.RepositoryFile;
import com.gitease.quickhub.repository.RepositoryRepository;
import com.gitease.quickhub.service.GiteeApiService;
import com.gitease.quickhub.service.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RepositoryServiceImpl implements RepositoryService {

    private final RepositoryRepository repositoryRepository;
    private final GiteeApiService giteeApiService;

    @Autowired
    public RepositoryServiceImpl(RepositoryRepository repositoryRepository, GiteeApiService giteeApiService) {
        this.repositoryRepository = repositoryRepository;
        this.giteeApiService = giteeApiService;
    }

    @Override
    public List<Repository> getUserRepositories(String accessToken) {
        List<Repository> repositories = giteeApiService.getUserRepositories(accessToken);
        // Save repositories to database
        return repositoryRepository.saveAll(repositories);
    }

    @Override
    public Repository getRepository(String accessToken, String owner, String repo) {
        return giteeApiService.getRepository(accessToken, owner, repo);
    }

    @Override
    public List<RepositoryFile> getRepositoryContents(String accessToken, String owner, String repo, String path, String ref) {
        return giteeApiService.getRepositoryContents(accessToken, owner, repo, path, ref);
    }

    @Override
    public RepositoryFile getRepositoryFile(String accessToken, String owner, String repo, String path, String ref) {
        return giteeApiService.getRepositoryFile(accessToken, owner, repo, path, ref);
    }

    @Override
    public List<Commit> getRepositoryCommits(String accessToken, String owner, String repo, String branch) {
        return giteeApiService.getRepositoryCommits(accessToken, owner, repo, branch);
    }

    @Override
    public Commit createCommit(String accessToken, String owner, String repo, String path, String content, String message, String branch) {
        return giteeApiService.createCommit(accessToken, owner, repo, path, content, message, branch);
    }

    @Override
    public List<String> getRepositoryBranches(String accessToken, String owner, String repo) {
        return giteeApiService.getRepositoryBranches(accessToken, owner, repo);
    }
}